# 文件名: ex_playwright4
# 当前用户: shiping.zheng
# 当前日期: 2025/1/10
# 当前时间: 9:41
# 项目名称: Flag
"""
模块描述:
    playwright异步执行多个不同任务,包含阻塞流程
"""
import asyncio
import time
from playwright.async_api import async_playwright
async def demo01(page):
    await page.goto('https://xuntian-test1.tclpv.com/getechLogin')
    for i in range(500):    # 模拟阻塞操作
        print(f"循环打印i {i}")
    print("打开巡天测试1")
    # 执行操作
    print(await page.title())
async def demo02(page):
    await page.goto('https://xuntian-test2.tclpv.com/getechLogin')
    print("打开巡天测试2")
    await page.goto('https://sp-gateway.tcl.com/oauth/login')
    print("打开OA系统")
    # 执行操作
    print(await page.title())
async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        context = await browser.new_context()
        # 创建页面
        page1 = await context.new_page()
        page2 = await context.new_page()
        # 创建任务列表，每个任务执行不同的函数
        tasks = [
            demo01(page1),
            demo02(page2),
        ]
        # 并发执行所有任务
        await asyncio.gather(*tasks)
        time.sleep(5)
        await browser.close()
# 运行异步主函数
asyncio.run(main())